package com.aod.estacionamentolivrex;

import jim.h.common.android.zxinglib.integrator.IntentIntegrator;
import android.R.anim;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Vibrator;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class TagActivity extends Activity {

	static int max = 1000;
	static int nums = 5;
	static int intervalo = 1500;
	
	private String value = null;
	static int valorSeek = 0;
	static String Part1 = "";
	static String Part2 = "";
	static boolean sair = false;
	
	TextView tag_bar;
	SeekBar seekBar_tags;
	View btn_iniciar;
	Thread processo;

	static Vibrator vibarar;
	boolean started = false;
	boolean reverse = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tag_fake);
		TagActivity.valorSeek = 0;
		vibarar = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
		 
		Bundle extras;
		if (savedInstanceState == null) {
		    extras = getIntent().getExtras();
		    if(value == null) {
		    	value = extras.getString("result");
		    } else {
		    	value = extras.getString("result");
		    }
		} else {
			value = (String) savedInstanceState.getSerializable("result");
		}
		
		this.tag_bar = (TextView)findViewById(R.id.tag_bar);
		this.seekBar_tags = (SeekBar)findViewById(R.id.seekBar_tags);
		this.btn_iniciar = (View) findViewById(R.id.bt_iniciar);
		
		FontUtils.setCustomFont(tag_bar, this.getAssets());
		tag_bar.setText(value);

		if(value.length() > nums)
		{
			Part1 = value.substring(0, (value.length()-nums));
			Part2 = value.substring(Part1.length(), value.length());
		}
		
		seekBar_tags.setMax(max);
		
		seekBar_tags.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				TagActivity.valorSeek = progress;

				if(value.length() > nums)
				{
					Part1 = value.substring(0, (value.length()-nums));
					Part2 = value.substring(Part1.length(), value.length());
					
					tag_bar.setText(Part1 + String.valueOf(Integer.parseInt(Part2)+progress));					
				}
				else
				{
					tag_bar.setText(String.valueOf(Integer.parseInt(value)+progress));
				}				
				
			}
		});		
		
					btn_iniciar.setOnClickListener(new OnClickListener() {
			            @Override
			            public void onClick(View v) {
			            	if(!started)
			            	{
			            		try {
						            	processo = new Thread(new Runnable() {
						                    public void run() {
						                    	started = true;						                    	
						                    	
						                    	while(started)
						                    	{ 
						                    		if(TagActivity.valorSeek == max)
						                    		{
						                    			 reverse = true;						                    			
						                    		}
						                    		if(TagActivity.valorSeek == 0)
						                    		{
						                    			 reverse = false;						                    			
						                    		}
							                    	if(TagActivity.valorSeek <= max && !reverse)
							                    	{
							                    		TagActivity.valorSeek++;
//							            				val = valOrig + 1;
							                    	}
							                    	
							                    	if(TagActivity.valorSeek >= 0 && reverse)
							                    	{
							                    		TagActivity.valorSeek--;
							                    	}

							                    	new UpdateSeekBarTask().execute(TagActivity.valorSeek);
							                    	
							                    	try {							                    		
														Thread.sleep(Long.valueOf(intervalo));
													} catch (InterruptedException e) {
														// TODO Auto-generated catch block
														e.printStackTrace();
													}
						                    		
						                    		
							                    	
							                    	
						                    	}
						            		
						                    	
						                    	
						                    							                    	
						                    }
						                });
						            	processo.start();
			        			} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			            	}
			            	else
			            	{			      

		                    	
		                    	try {
		                    		started = false;
				            		processo.stop();
				            		processo.destroy();
				            		processo = null;
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
	                    		
		                    	
			            		
			            	}
			            	
			            	
			            }
			        });		
		
	}

	public void onDestroy()
	{
		started = false;
		super.onDestroy();
	}
	
	private class UpdateSeekBarTask extends AsyncTask<Integer, Void, View> {
		Integer valor = 0;
		
		protected View doInBackground(Integer... urls) {
			valor = urls[0];
			
	        return null;
	    }
	    
	    /** The system calls this to perform work in the UI thread and delivers
	      * the result from doInBackground() */
	    protected void onPostExecute(View result) {
	    	
	    	try
	    	{
    			vibarar.vibrate(80);
	    		
	        	if(value.length() > nums)
				{
					Part1 = value.substring(0, (value.length()-nums));
					Part2 = value.substring(Part1.length(), value.length());
					
					tag_bar.setText(Part1 + String.valueOf(Integer.parseInt(Part2)+valor));					
				}
				else
				{
					tag_bar.setText(String.valueOf(Integer.parseInt(value)+valor));
				}		
	        		        	
		    	seekBar_tags.setProgress(valor);
		    	
	    	}
	    	catch(Exception e)
	    	{
	    		e.printStackTrace();
	    	}
	    }
	}
	
}
